
window.onload = function () {
 

    ajaxLoading();
}




var onlyOpenTitle = "欢迎使用";//不允许关闭的标签的标题

var _menus;
 
var _notice_url;
var _notice_title;
var _notice_icon;

//采用jquery easyui loading css效果 
function ajaxLoading() {
    
    //$("<div class=\"datagrid-mask\"></div>").css({ display: "block", width: "100%", height: $(window).height() }).appendTo("body");
    //$("<div class=\"datagrid-mask-msg\"></div>").html($("#loading").val()).appendTo("body").css({ display: "block", left: ($(document.body).outerWidth(true) - 190) / 2, top: ($(window).height() - 45) / 2 });

    $.post('/Home/GetTreeByRole',
             function (data) {
                 _menus = data;

                 //  _menus = {
                 //    "menus": [{
                 //        "menuid": "1",
                 //        "icon": "icon-sys",
                 //        "menuname": "后台统计",
                 //        "menus": [ 
                 //         {
                 //             "menuid": "3333",
                 //             "menuname": "周计划导入",
                 //             "icon": "icon-role",
                 //             "url": "/../zh-CN/PlanManage/Index"
                 //         }]
                 //    }
                 //    , {
                 //        "menuid": "1",
                 //        "icon": "icon-sys",
                 //        "menuname": "基本信息",
                 //        "menus": [ 
                 //         {
                 //             "menuid": "2222",
                 //             "menuname": "用户管理",
                 //             "icon": "icon-role",
                 //             "url": "/../zh-CN/Userlist/Index"
                 //         }]
                 //    }]
                 //}


                 //InitLeftMenu();

                 //ajaxLoadEnd();

             });

};
 
function ajaxLoadEnd() {
     
    $(".datagrid-mask").remove();
    $(".datagrid-mask-msg").remove();
}

$(function () { 

    tabClose();

    tabCloseEven();

    /* 选择TAB时刷新内容
        $('#tabs').tabs({
            onSelect: function (title) {
                var currTab = $('#tabs').tabs('getTab', title);
                var iframe = $(currTab.panel('options').content);
    
                var src = iframe.attr('src');
                if(src)
                    $('#tabs').tabs('update', { tab: currTab, options: { content: createFrame(src)} });
    
            }
        });
    */

})

//初始化左侧
function InitLeftMenu() {
    $("#nav").accordion({ animate: false, fit: true, border: false });
    var selectedPanelname = '';
    $.each(_menus.menus, function (i, n) {

        var menulist = '';
        menulist += '<ul class="navlist">';
 
        $.each(n.menus, function (j, o) {
 
            if (o.menuid == 'NoticeSearch') {
                _notice_title = o.menuname;
                _notice_url = o.url;
                _notice_icon = o.icon;
            }
            
            menulist += '<li><div ><a ref="' + o.menuid + '" href="#" rel="' + o.url + '" ><span class="icon ' + o.icon + '" >&nbsp;</span><span class="nav">' + o.menuname + '</span></a></div> ';
             
            if (o.child && o.child.length > 0) {
                //li.find('div').addClass('icon-arrow');
          
                menulist += '<ul class="third_ul">';
                $.each(o.child, function (k, p) { 
                    menulist += '<li><div><a ref="' + p.menuid + '" href="#" rel="' + p.url + '" ><span class="icon ' + p.icon + '" >&nbsp;</span><span class="nav">' + p.menuname + '</span></a></div> </li>'
                });
                menulist += '</ul>';
            }

            menulist += '</li>';
        })

        menulist += '</ul>';

        $('#nav').accordion('add', {
            title: n.menuname,
            content: menulist, 
            border: false,
            iconCls: 'icon ' + n.icon 
        });

       
        if (i == 0)
            selectedPanelname = n.menuname;

    });

    $('#nav').accordion('select', selectedPanelname);

 
    $('.navlist li a').click(function () {
        var tabTitle = $(this).children('.nav').text();

        var url = $(this).attr("rel");
        var menuid = $(this).attr("ref");
        var icon = $(this).find('.icon').attr('class');

        var third = find(menuid);
        if (third && third.child && third.child.length > 0) {
            $('.third_ul').slideUp();

            var ul = $(this).parent().next();
            if (ul.is(":hidden"))
                ul.slideDown();
            else
                ul.slideUp(); 

        }
        else {
            addTab(tabTitle, url, icon);
            $('.navlist li div').removeClass("selected");
            $(this).parent().addClass("selected");
        }
    }).hover(function () {
        $(this).parent().addClass("hover");
    }, function () {
        $(this).parent().removeClass("hover");
    });

 
    //选中第一个
    //var panels = $('#nav').accordion('panels');
    //var t = panels[0].panel('options').title;
    //$('#nav').accordion('select', t);
}
//获取左侧导航的图标
function getIcon(menuid) {
    var icon = 'icon ';
    $.each(_menus.menus, function (i, n) {
        $.each(n.menus, function (j, o) {
            if (o.menuid == menuid) {
                icon += o.icon;
            }
        })
    })

    return icon;
}

function find(menuid) {
    var obj = null;
    $.each(_menus.menus, function (i, n) {
        $.each(n.menus, function (j, o) {
            if (o.menuid == menuid) {
                obj = o;
            }
        });
    });

    return obj;
}

function addTab(subtitle, url, icon) {
    if (!$('#tabs').tabs('exists', subtitle)) {
        $('#tabs').tabs('add', {
            title: subtitle,
            content: createFrame(url),
            closable: true,
            icon: icon
        });
    } else {
        $('#tabs').tabs('select', subtitle);
        $('#mm-tabupdate').click();
    }
    tabClose();
}

 
function createFrame(url) {
    var s = '<iframe frameborder="0" src="' + url + '" scrolling="auto" style="width:100%; height:99%"></iframe>';
    return s;
}

function tabClose() {
    /*双击关闭TAB选项卡*/
    $(".tabs-inner").dblclick(function () {
        var subtitle = $(this).children(".tabs-closable").text();
        $('#tabs').tabs('close', subtitle);
    })
    /*为选项卡绑定右键*/
    $(".tabs-inner").bind('contextmenu', function (e) {
        $('#mm').menu('show', {
            left: e.pageX,
            top: e.pageY
        });

        var subtitle = $(this).children(".tabs-closable").text();

        $('#mm').data("currtab", subtitle);
        $('#tabs').tabs('select', subtitle);
        return false;
    });
}


//绑定右键菜单事件
function tabCloseEven() {

    $('#mm').menu({
        onClick: function (item) {
            closeTab(item.id);
        }
    });

    return false;
}
 
function GotoNotice() {
 
    if (_notice_title == null || _notice_title == '') return;
 
    addTab(_notice_title, _notice_url, _notice_icon);

    $('.navlist li div').removeClass("selected");
    $(this).parent().addClass("selected");
}

function closeTab(action) {

    var alltabs = $('#tabs').tabs('tabs');
    var currentTab = $('#tabs').tabs('getSelected');
    var allTabtitle = [];

    $.each(alltabs, function (i, n) {
        allTabtitle.push($(n).panel('options').title);
    })

    switch (action) {
        case "refresh":
            var iframe = $(currentTab.panel('options').content);
            var src = iframe.attr('src');
            $('#tabs').tabs('update', {
                tab: currentTab,
                options: {
                    content: createFrame(src)
                }
            })
            break;
        case "close":
            var currtab_title = currentTab.panel('options').title;
            $('#tabs').tabs('close', currtab_title);
            break;
        case "closeall":
            $.each(allTabtitle, function (i, n) {
                if (n != onlyOpenTitle) {
                    $('#tabs').tabs('close', n);
                }
            });
            break;
        case "closeother":
            var currtab_title = currentTab.panel('options').title;
            $.each(allTabtitle, function (i, n) {
                if (n != currtab_title && n != onlyOpenTitle) {
                    $('#tabs').tabs('close', n);
                }
            });
            break;
        case "closeright":
            var tabIndex = $('#tabs').tabs('getTabIndex', currentTab);

            if (tabIndex == alltabs.length - 1) {
                alert('亲，后边没有啦 ^@^!!');
                return false;
            }
            $.each(allTabtitle, function (i, n) {
                if (i > tabIndex) {
                    if (n != onlyOpenTitle) {
                        $('#tabs').tabs('close', n);
                    }
                }
            });

            break;
        case "closeleft":
            var tabIndex = $('#tabs').tabs('getTabIndex', currentTab);
            if (tabIndex == 1) {
                alert('亲，前边那个上头有人，咱惹不起哦。 ^@^!!');
                return false;
            }
            $.each(allTabtitle, function (i, n) {
                if (i < tabIndex) {
                    if (n != onlyOpenTitle) {
                        $('#tabs').tabs('close', n);
                    }
                }
            });

            break;
        case "exit":
            $('#closeMenu').menu('hide');
            break;
    }
}


Date.isLeapYear = function (year) {
    return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
};

Date.getDaysInMonth = function (year, month) {
    return [31, (Date.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
};

Date.prototype.isLeapYear = function () {
    return Date.isLeapYear(this.getFullYear());
};

Date.prototype.getDaysInMonth = function () {
    return Date.getDaysInMonth(this.getFullYear(), this.getMonth());
};

Date.prototype.addMonths = function (value) {
    var n = this.getDate();
    this.setDate(1);
    this.setMonth(this.getMonth() + value);
    this.setDate(Math.min(n, this.getDaysInMonth()));
    return this;
};
function addDays(today, days) {
    var a = today.valueOf()
    a = a + (days * 24 * 60 * 60 * 1000)
    a = new Date(a)
    return a;
}
function formatDate(curr_time) {
    try {
        var strDate = curr_time.getFullYear() + "-";
        strDate += curr_time.getMonth() + 1 + "-";
        strDate += curr_time.getDate()
        // //console.log(strDate);
        return strDate
    } catch (e) {
        return curr_time
    }
}
function formatDateTime(curr_time) {
    try {
        var strDate = curr_time.getFullYear() + "-";
        strDate += curr_time.getMonth() + 1 + "-";
        strDate += curr_time.getDate() + " ";
        strDate += curr_time.getHours() + ":";
        strDate += curr_time.getMinutes() + ":";
        strDate += curr_time.getSeconds();
        // //console.log(strDate);
        return strDate
    } catch (e) {
        return curr_time
    }

}